package com.engiu.gwt.sample.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.engiu.gwt.sample.shared.FieldVerifier;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.ClosingEvent;
import com.google.gwt.user.client.Window.ClosingHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle.MultiWordSuggestion;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.SuggestBox.DefaultSuggestionDisplay;
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class SampleGWT implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		final Button sendButton = new Button("Send");
		

		final MultipleTextBox nameField1 = new MultipleTextBox();
		final SuggestBox nameField = new SuggestBox(new MultiWordSuggestOracle(), nameField1);
		final DefaultSuggestionDisplay display = (DefaultSuggestionDisplay)nameField.getSuggestionDisplay();

		display.setSuggestionListHiddenWhenEmpty(true);
		nameField1.setWidth("300px");
		
		Event.addNativePreviewHandler(new NativePreviewHandler(){

			@Override
			public void onPreviewNativeEvent(NativePreviewEvent event) {
				//CTRL + SPACE keys
				if(event.getNativeEvent().getCtrlKey()){
					if(event.getNativeEvent().getKeyCode() == 32){
						nameField.showSuggestionList();
					}
				}
			}
			
		});
		
		nameField.addKeyUpHandler(new KeyUpHandler(){

			@Override
			public void onKeyUp(KeyUpEvent event) {
				
				switch(event.getNativeKeyCode()){
					case KeyCodes.KEY_ESCAPE:
						display.hideSuggestions();
						break;
					case 107: // = keys
						// check valid variable
						String variable = null;
						if((variable = nameField1.getValidVariable()) != null){
							MultiWordSuggestOracle oracle = ((MultiWordSuggestOracle)nameField.getSuggestOracle());
							oracle.clear();
							oracle.addAll(getVarSuggestions(variable));
							oracle.setDefaultSuggestionsFromText(getVarSuggestions(variable));
							nameField.showSuggestionList();
						}
						break;
				}
			}
		});
		
		nameField.addValueChangeHandler(new ValueChangeHandler<String>(){

			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				MultiWordSuggestOracle oracle = ((MultiWordSuggestOracle)nameField.getSuggestOracle());
				oracle.clear();
				oracle.setDefaultSuggestionsFromText(new ArrayList<String>(0));
			}
			
		});
		
		nameField.setText("");

		// We can add style names to widgets
		sendButton.addStyleName("sendButton");

		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		RootPanel.get("nameFieldContainer").add(nameField);
		RootPanel.get("sendButtonContainer").add(sendButton);

		// Focus the cursor on the name field when the app loads
		nameField.setFocus(true);
		nameField.getTextBox().selectAll();

	}
	public static List<String> getVarSuggestions(String variable){
		HashMap<String, List<String>> map = new HashMap<String, List<String>>();
		map.put("var1", getVar1Suggestions());
		map.put("var2", getVar2Suggestions());
		
		return map.get(variable);
	}
	
	public static List<String> getVar1Suggestions(){
		List<String> list = new ArrayList<String>();
		list.add("AmyKesic");
		list.add("JasonWeston");
		list.add("DaveJohnson");
		list.add("PaulHammant");
		list.add("JesseKuhnert");
		list.add("BenAlex");
		list.add("TomBender");
		list.add("AlexandruPopescu");
		list.add("KavehArabfakhry");
		list.add("StevenHong");
		
		return list;
	}
	
	public static List<String> getVar2Suggestions(){
		List<String> list = new ArrayList<String>();
		list.add("CandyChastainMielke");
		list.add("ScottMark");
		list.add("DovB.Katz");
		list.add("AlefArendsen");
		list.add("DavidJencks");
		list.add("AlexeyBelikov");
		list.add("BryanVial");
		list.add("DrorBereznitsky");
		list.add("DavidMoskowitz");
		list.add("OscarChan");
		list.add("SergeySundukovskiy");
		list.add("JohnNewton");
		
		return list;
	}
}
